<template>
	<view class="box">
		<!-- 空状态 -->
		<view v-if="collectionList.length === 0" class="empty-state">
			<text class="empty-icon">⭐</text>
			<text class="empty-text">暂无收藏</text>
			<text class="empty-tip">快去收藏你喜欢的房源吧~</text>
		</view>

		<!-- 收藏列表 -->
		<view v-else>
			<view class="content" v-for="(house, index) in collectionList" :key="index" @click="goToDetail(house)">
				<view class="left">
					<image class="imgs" :src="house.image" mode="aspectFill"></image>
				</view>
				<view class="right">
					<p class="first">{{ house.title }}</p>
					<p class="start">{{ house.roomType }}/{{ house.size }}平/{{ house.oriented }}/{{ house.community }}</p>
					<p class="last">
						<span>{{ house.price }}</span>
						<span class="five">元/月</span>
					</p>
				</view>
				<view class="delete-btn" @click.stop="removeCollection(house, index)">
					<text>✕</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { baseUrl } from '@/context/config.js'
	export default {
		data() {
			return {
				collectionList: [],
				loading: false, // 加载状态
			}
		},
		onShow() {
			// 每次显示页面时重新加载收藏列表
			this.loadCollections();
		},
		methods: {
		// 加载收藏列表
		loadCollections() {
			this.loading = true;
			try {
				// 获取存储的token
				const token = uni.getStorageSync('token');
				
				// 如果没有token，提示用户登录
				if (!token) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					});
					this.loading = false;
					return;
				}
				
				// 发送请求到接口获取收藏列表
		uni.request({
			url: `${baseUrl}/user/favorites`,
			method: 'GET',
			header: {
				'Authorization': token
			},
					success: (res) => {
						console.log('收藏列表数据:', res);
						if (res.statusCode === 200 && res.data && res.data.status === 200) {
							const favorites = res.data.body || [];
							// 将接口返回的数据转换为页面需要的格式
							this.collectionList = favorites.map(item => {
								// 解析desc字段获取户型、面积、朝向、小区信息
								const descParts = (item.desc || '').split('/');
								// 处理图片URL，确保使用完整域名
					let imageUrl = item.houseImg || '';
					if (imageUrl && !imageUrl.startsWith('http')) {
						imageUrl = `${baseUrl}${imageUrl.startsWith('/') ? '' : '/'}${imageUrl}`;
					}
								return {
									image: imageUrl,
									title: item.title || '',
									roomType: descParts[0] || '',
									size: descParts[1] || '',
									oriented: descParts[2] || '',
									community: descParts[3] || '',
									price: item.price || 0,
									houseCode: item.houseCode || ''
								};
							});
						} else {
							this.collectionList = [];
							uni.showToast({
								title: res.data?.description || '获取收藏列表失败',
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						console.error('获取收藏列表失败:', err);
						this.collectionList = [];
						uni.showToast({
							title: '网络错误，请重试',
							icon: 'none'
						});
					},
					complete: () => {
						this.loading = false;
					}
				});
			} catch (e) {
				console.error('加载收藏列表失败:', e);
				this.collectionList = [];
				this.loading = false;
			}
		},

			// 删除收藏
		removeCollection(house, index) {
			uni.showModal({
				title: '提示',
				content: '确定要取消收藏吗？',
				success: (res) => {
					if (res.confirm) {
						// 获取token
						const token = uni.getStorageSync('token');
						if (!token) {
							uni.showToast({
								title: '请先登录',
								icon: 'none'
							});
							return;
						}
						
						// 调用接口删除收藏
			uni.request({
				url: `${baseUrl}/user/favorites/${house.houseCode}`,
				method: 'DELETE',
				header: {
					'Authorization': token
				},
							success: (res) => {
								console.log('删除收藏响应:', res);
								if (res.statusCode === 200 && res.data && res.data.status === 200) {
									// 更新本地列表
									this.collectionList.splice(index, 1);
									
									uni.showToast({
										title: '已取消收藏',
										icon: 'success'
									});
								} else {
									uni.showToast({
										title: res.data?.description || '取消收藏失败',
										icon: 'none'
									});
								}
							},
							fail: (err) => {
								console.error('删除收藏失败:', err);
								uni.showToast({
									title: '网络错误，请重试',
									icon: 'none'
								});
							}
						});
					}
				}
			});
		},

			// 跳转到详情页
			goToDetail(house) {
				uni.navigateTo({
					url: `/ulit/detalispublish/detalispublish?houseCode=${house.houseCode}`
				});
			}
		}
	}
</script>

<style scoped>
	.box {
		width: 100%;
		min-height: 100vh;
		background-color: #f5f5f5;
		padding: 20rpx;
	}

	/* 空状态 */
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding-top: 200rpx;
	}

	.empty-icon {
		font-size: 120rpx;
		margin-bottom: 30rpx;
	}

	.empty-text {
		font-size: 32rpx;
		color: #999;
		margin-bottom: 15rpx;
	}

	.empty-tip {
		font-size: 26rpx;
		color: #ccc;
	}

	/* 收藏卡片 */
	.content {
		position: relative;
		display: flex;
		background-color: #fff;
		margin-bottom: 20rpx;
		padding: 20rpx;
		border-radius: 12rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.06);
	}

	.left {
		width: 240rpx;
		height: 180rpx;
		margin-right: 20rpx;
		flex-shrink: 0;
	}

	.imgs {
		width: 100%;
		height: 100%;
		border-radius: 8rpx;
	}

	.right {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding-right: 60rpx;
	}

	.first {
		font-weight: 600;
		font-size: 32rpx;
		color: #333;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		line-height: 1.4;
		margin-bottom: 10rpx;
	}

	.start {
		font-size: 26rpx;
		color: #999;
		margin-bottom: 10rpx;
	}

	.last {
		display: flex;
		align-items: baseline;
	}

	.last > span {
		color: #FF5252;
		font-weight: 900;
		font-size: 40rpx;
	}

	.five {
		font-size: 24rpx !important;
		margin-left: 4rpx;
	}

	/* 删除按钮 */
	.delete-btn {
		position: absolute;
		top: 20rpx;
		right: 20rpx;
		width: 50rpx;
		height: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(0, 0, 0, 0.5);
		color: #fff;
		border-radius: 50%;
		font-size: 30rpx;
		font-weight: bold;
	}
</style>